package paymentplan

import (
	// "net/http"
	"encoding/json"
	"strconv"
	"time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"
	
	"oxy_server/module/log"
	"oxy_server/model"
	"oxy_server/until"
)

type (
	paymentplan struct {
		ID bson.ObjectId `json:"_id" bson:"_id"`
		ContractNumber 					string `json:"contractnumber" bson:"contractnumber"`			//合同编号
		SupplierName						string `json:"suppliername" bson:"suppliername"`						//供应商
		Brand						string `json:"brand" bson:"brand"`						//品牌
		ContractType						string `json:"contracttype" bson:"contracttype"`						//类型
		Supplier						string `json:"supplier" bson:"supplier"`						//供应商
		OrderMoney						string `json:"ordermoney" bson:"ordermoney"`					//下单金额
		OrderTime						time.Time `json:"ordertime" bson:"ordertime"`					//下单时间
		PaymentPlan					  	string `json:"paymentplan" bson:"paymentplan"`				//付款计划
		Evidence						string `json:"evidence" bson:"evidence"`						//凭证
		Note							string `json:"note" bson:"note"`								//备注
	}
	insertpaymentplan struct {
		ContractNumber 					string `json:"contractnumber" bson:"contractnumber"`			//合同编号
		SupplierName						string `json:"suppliername" bson:"suppliername"`						//供应商
		Supplier						string `json:"supplier" bson:"supplier"`						//供应商	
		Brand						string `json:"brand" bson:"brand"`						//品牌
		ContractType						string `json:"contracttype" bson:"contracttype"`						//类型
		OrderMoney						string `json:"ordermoney" bson:"ordermoney"`					//下单金额
		OrderTime						time.Time `json:"ordertime" bson:"ordertime"`					//下单时间
		PaymentPlan						string `json:"paymentplan" bson:"paymentplan"`				//付款计划
		Evidence						string `json:"evidence" bson:"evidence"`						//凭证
		Note							string `json:"note" bson:"note"`								//备注
	}
)

type OXYPaymentPlanData struct{}

func NewOXYPaymentPlanData() *OXYPaymentPlanData{
	paymentPlanData:= &OXYPaymentPlanData{}
	return paymentPlanData
}

func (opd *OXYPaymentPlanData) GetPaymentPlanListHandler(c echo.Context) error {
	currentpage,err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage,err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="payment_plan"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	query,count,err:=model.DB().QuerySort(M, currentpage, perpage,"-_id")
	if err != nil {
		log.Error("查询出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	ps:=[]paymentplan{}
	query.All(&ps)
	
	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":string(str),
		"count":count,
	})

	return nil
}

func (opd *OXYPaymentPlanData) AddPaymentPlanHandler(c echo.Context) error {
	o:= &insertpaymentplan{}
	if err := c.Bind(o); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}

	model.DB().Dbname="OxyCms"
	model.DB().Document="payment_plan"
	err:=model.DB().Insert(o)
	if err != nil {
		log.Error("插入出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
	})

	return nil	
}
func (opd *OXYPaymentPlanData) UpdatePaymentPlanHandler(c echo.Context) error {
	id := c.QueryParam("id") 
	log.Info("UpdatePaymentPlanHandler",id)
	
	o:= &insertpaymentplan{}
	if err := c.Bind(o); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	
	model.DB().Dbname="OxyCms"
	model.DB().Document="payment_plan"
	err:= model.DB().Update(bson.M{"_id":bson.ObjectIdHex(id)} , o)
	if err != nil{
		log.Error("更新出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}
func (opd *OXYPaymentPlanData) DelPaymentPlanHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelPaymentPlanHandler",id)
	model.DB().Dbname="OxyCms"
	model.DB().Document="payment_plan"
	err:=model.DB().Remove(bson.M{"_id":bson.ObjectIdHex(id)})
	if err != nil{
		log.Error("删除出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":  "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}